// Copyright (c) 2008-2010, Arne Claus
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
// following conditions are met:
//
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following 
//   disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
//   disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name Arne Claus nor the names of any contributors may be used to endorse or promote products
//   derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef __INCLUDED_CBL_CPU_H__
#define __INCLUDED_CBL_CPU_H__

#include "CBL_Defines.h"
#include <cstdlib>

CB_SUBNAMESPACE_START(Cpu)

// ---------------------------------------------------------------------------------------------
//  Basic data
// ---------------------------------------------------------------------------------------------

enum SIMDType		{ SIMDUnknown=0, NoSIMD, MMX, SSE, SSE2, SSE3, SSE41, SSE42, Altivec };
enum CpuType		{ CpuTypeUnknown=0, Intel, AMD, PowerPC };

struct SCpuInfo
{
	CpuType         Type;				//!< Processor type
	SIMDType        SIMD;				//!< SIMD type supported by this procesor
	uint32          Processors;			//!< Number of physical processors
	uint32          Cores;				//!< Number of cores
	uint32          LogicalUnits;		//!< e.g. hyper threading
	
	SCpuInfo()
	: Type(CpuTypeUnknown)
	, SIMD(SIMDUnknown)
	, Processors(0)
	, Cores(0)
	, LogicalUnits(0)
	{}
};

// -------------------------------------------------------------------------------------------------

//! \brief Returns the SCpuInfo struct for the current processor
BASELIB_API const SCpuInfo& GetCpuInfo();

//! \brief Returns the total number of physical and logical CPUs
CB_FORCEINLINE uint32 CB_INLINE_ATTR GetTotalNumberOfProcessors();

//! \brief Returns the total number of "real" Cpus (exclude logical units)
CB_FORCEINLINE uint32 CB_INLINE_ATTR GetTotalNumberOfPhysicalProcessors();

//! \brief  Thread-Sleep for the time given.
//! \detail Might exit after a longer time than given but never earlier.
BASELIB_API void Wait(uint32 _NumMs);

//! \brief  Thread-Sleep for the time given.
//! \detail Sleeps the thread until _NumMs or the thread is being signaled.
//!			Might exit after a longer time, depending on system responsiveness.
BASELIB_API void Sleep(uint32 _NumMs);

//! \return The current System time in Milliseconds.
BASELIB_API double GetTimeMs();

// -------------------------------------------------------------------------------------------------
//	x86 only functions. These will have no effect on other architectures
// -------------------------------------------------------------------------------------------------

namespace x86
{
	//! \brief  Call when done with MMX functions.
	//! \detail Only available on x86 CPUs. Quite expensive, so use only when necessary.
    BASELIB_API void FlushMMXRegister();	
    
	//! \brief  Set the FPU to fast, but inprecise.
	//! \detail Only available on x86 CPUs.
    BASELIB_API void SetFastFPU();
	
    //! \brief  Set the FPU to slow, but precise.
	//! \detail Only available on x86 CPUs.
    BASELIB_API void SetPreciseFPU();
	
    //! \brief  Enable SSE acceleration for CRT functions.
	//! \detail Only available on x86 CPUs.
    BASELIB_API void EnableCRTSSE();
	
    //! \brief  Disable SSE acceleration for CRT functions.
	//! \detail Only available on x86 CPUs.
    BASELIB_API void DisableCRTSSE();
}

#include "CBL_Cpu.hpp"

CB_SUBNAMESPACE_END

#endif